(function() {
    Path.on("/main/__basename__.html", function(_current_location) {
        var qs = _current_location.query;
        var section_id = qs.get("section_id");
        var news_url = section_id ? (public_api.news.get.list_by_section_id + section_id) : public_api.news.get.list_all;
        console.log(news_url)
            // 拉取数据列表
        coAjax.get(public_api.section.get.list_all, function(result) {
            App.set("$Cache.main.articlemanager.section_list", result);
        });
        coAjax.get(news_url, function(result) {
            App.set("$Cache.main.articlemanager.article_list", result);
            console.log(result);
            alert("success", "更新文章列表成功");
        });
    });
    // 更换文章模块加载文章列表
    App.set("$Event.main.articlemanager.changemodule", function(e, vm) {
        Path.jump("/main/articlemanager.html?section_id=" + vm.get("_id"));
    });
    // 开启模块修改
    App.set("$Event.main.articlemanager.openeditormodule", function(e, vm) {
        vm.model.toggle("$Cache.main.articlemanager.openeditormodule");
    });
    // 模块修改
    App.set("$Event.main.articlemanager.editormodule", function(e, vm) {
        coAjax.put(public_api.section.update, vm.get(), function(result) {
            alert("success", "修改板块名称成功");
            vm.set("$Cache.main.articlemanager.openeditormodule", false);
        });
    });
    /*
     * 删除模块，应同时删除模块下所有文章
     */
    App.set("$Event.main.articlemanager.deletemodule", function(e, vm) {
        myConfirm("删除模块将会删除此模块下的所有文章内容，点击确定前请思考清楚。", function() {
            coAjax.delete(public_api.section.remove + vm.get("_id"), function(result) {
                vm.remove();
                alert("success", "删除板块成功");
                Path.jump("/main/articlemanager.html");
            });
        });
    });
    // 打开文章编辑
    App.set("$Event.main.articlemanager.openeditorarticle", function(e, vm) {
        // Path.jump("/main/articleedit.html?_id=" + vm.get("_id"));
        window.open(window.location.origin + "/main/articleedit.html?_id=" + vm.get("_id"));
    });
    // 删除文章
    App.set("$$Event.main.articlemanager.deletearticle", function(e, vm) {
        myConfirm("确定要删除《" + vm.get("title") + "》这篇文章吗？", function() {
            coAjax.delete(public_api.news.remove + vm.get("_id"), function() {
                vm.remove();
                alert("success", "删除文章成功");
            });
        });
    });
}());